<?php
use App\Exceptions\ReqException;
/**
 * 获取配置文件路径
 */
if (! function_exists ( 'config_path' )) {
	/**
	 * Get the configuration path.
	 *
	 * @param string $path        	
	 * @return string
	 */
	function config_path($path = '') {
		return app ()->basePath () . '/config' . ($path ? '/' . $path : $path);
	}
}

/**
 * 将对象转化为数组
 *
 * @param unknown $obj        	
 * @return unknown|multitype:NULL unknown
 */
function object2array($obj) {
	// 如果$obj是数组,格式化数组
	if (is_array ( $obj )) {
		return format_array ( $obj );
	}
	// 如果$obj不是对象，也不是数组，说明是基本类型，直接返回
	if (! is_object ( $obj )) {
		return $obj;
	}
	$ret = [ ];
	// 获取对象所有public属性
	$attributes = get_object_vars ( $obj );
	// 遍历所有属性
	foreach ( $attributes as $key => $value ) {
		if (is_object ( $value )) {
			// 如果$value是对象，将对象转化为数组
			$ret [$key] = object2array ( $value );
		} elseif (is_array ( $value )) {
			// 如果$value是数组，格式化数组
			$ret [$key] = format_array ( $value );
		} else {
			// $value是基本类型直接附加到结果数组中
			$ret [$key] = $value;
		}
	}
	return $ret;
}

/**
 * 将数组中的对象转化为数组形式
 *
 * @param unknown $arr        	
 * @return multitype:Ambigous <unknown, multitype:NULL > unknown
 */
function format_array($arr) {
	$arr2 = [ ];
	// 保存数组副本
	foreach ( $arr as $key => $value ) {
		$arr2 [$key] = $value;
	}
	// 遍历数组元素
	foreach ( $arr as $key => $value ) {
		if (is_array ( $value )) {
			// 如果$value是数组，格式化数组
			$arr2 [$key] = format_array ( $value );
		}
		if (is_object ( $value )) {
			// 如果$value是对象，转化为数组
			$arr2 [$key] = object2array ( $value );
		}
	}
	return $arr2;
}

/**
 * 返回数据
 *
 * @param array $array        	
 * @param string $respCode        	
 * @param string $respMsg        	
 * @param number $status        	
 * @param array $headers        	
 */
function responseData($data, $respCode = '0000', $respMsg = 'success', $status = 200, array $headers = []) {
	$result = [ 
			'respCode' => $respCode,
			'respMsg' => $respMsg,
			'data' => $data 
	];
	$jsonStr = json_encode ( $result, JSON_UNESCAPED_UNICODE );
	return response ( $jsonStr, $status, $headers );
}

/**
 * 返回列表数据
 * 
 * @param unknown $data        	
 * @param unknown $count        	
 * @param string $respCode        	
 * @param string $respMsg        	
 * @param number $status        	
 * @param array $headers        	
 */
function responseListData($data, $count, $respCode = '0000', $respMsg = 'success', $status = 200, array $headers = []) {
	$result = [ 
			'respCode' => $respCode,
			'respMsg' => $respMsg,
			'count' => $count,
			'data' => $data 
	];
	$jsonStr = json_encode ( $result, JSON_UNESCAPED_UNICODE );
	return response ( $jsonStr, $status, $headers );
}

/**
 * 返回错误信息
 *
 * @param string $respCode        	
 * @param string $respMsg        	
 * @param number $status        	
 * @param array $headers        	
 */
function responseError($respCode = '0000', $respMsg = 'success', $status = 200, array $headers = []) {
	$result = [ 
			'respCode' => $respCode,
			'respMsg' => $respMsg 
	];
	$jsonStr = json_encode ( $result, JSON_UNESCAPED_UNICODE );
	return response ( $jsonStr, $status, $headers );
}

/**
 * 返回html页面
 *
 * @param unknown $path        	
 */
function responseHtml($path) {
	return file_get_contents ( $path );
}

/**
 * http post请求
 *
 * @param unknown $url        	
 * @param unknown $senddata        	
 * @return unknown
 */
function http_post($url, $senddata) {
	try {
		$curl = curl_init ();
		curl_setopt ( $curl, CURLOPT_URL, $url );
		curl_setopt ( $curl, CURLOPT_HEADER, 0 );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $curl, CURLOPT_POST, 1 );
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $senddata );
		$recvdata = curl_exec ( $curl );
		if (curl_errno ( $curl )) {
			throw new ReqException ( '9003', curl_error ( $curl ) );
		}
		curl_close ( $curl );
		return $recvdata;
	} catch ( \Exception $ex ) {
		throw new ReqException ( '9003', $ex->getMessage () );
	}
}

/**
 * http get请求
 *
 * @param unknown $url        	
 * @return unknown
 */
function http_get($url) {
	try {
		$curl = curl_init ();
		curl_setopt ( $curl, CURLOPT_URL, $url );
		curl_setopt ( $curl, CURLOPT_HEADER, 0 );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		$data = curl_exec ( $curl );
		if (curl_errno ( $curl )) {
			throw new ReqException ( '9003', curl_error ( $curl ) );
		}
		curl_close ( $curl );
		return $data;
	} catch ( \Exception $ex ) {
		throw new ReqException ( '9003', $ex->getMessage () );
	}
}

/**
 * https post请求
 *
 * @param unknown $url        	
 * @param unknown $senddata        	
 * @param unknown $vertify        	
 * @return unknown
 */
function https_post($url, $senddata, $vertify = false) {
	try {
		$curl = curl_init ();
		curl_setopt ( $curl, CURLOPT_URL, $url );
		curl_setopt ( $curl, CURLOPT_HEADER, 0 );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $curl, CURLOPT_POST, 1 );
		if ($vertify) {
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, true );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
			curl_setopt ( $curl, CURLOPT_CAINFO, dirname ( __FILE__ ) . '/cacert.pem' );
		} else {
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
		}
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $senddata );
		$recvdata = curl_exec ( $curl );
		if (curl_errno ( $curl )) {
			throw new ReqException ( '9003', curl_error ( $curl ) );
		}
		curl_close ( $curl );
		return $recvdata;
	} catch ( \Exception $ex ) {
		throw new ReqException ( '9003', $ex->getMessage () );
	}
}

/**
 * https get请求
 *
 * @param unknown $url        	
 * @param string $vertify        	
 * @return unknown
 */
function https_get($url, $vertify = false) {
	try {
		$curl = curl_init ();
		curl_setopt ( $curl, CURLOPT_URL, $url );
		curl_setopt ( $curl, CURLOPT_HEADER, 0 );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		if ($vertify) {
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, true );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
			curl_setopt ( $curl, CURLOPT_CAINFO, dirname ( __FILE__ ) . '/cacert.pem' );
		} else {
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
		}
		$data = curl_exec ( $curl );
		if (curl_errno ( $curl )) {
			throw new ReqException ( '9003', curl_error ( $curl ) );
		}
		curl_close ( $curl );
		return $data;
	} catch ( \Exception $ex ) {
		throw new ReqException ( '9003', $ex->getMessage () );
	}
}

/**
 * 生成随机字符串
 *
 * @param number $length        	
 * @return string
 */
function create_nonce_str($length = 16) {
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	$str = "";
	for($i = 0; $i < $length; $i ++) {
		$str .= substr ( $chars, mt_rand ( 0, strlen ( $chars ) - 1 ), 1 );
	}
	return $str;
}

/**
 * 判断数组是否存在某个value值
 *
 * @param array $arr        	
 * @param unknown $value        	
 * @return boolean
 */
function array_contain_value(array $arr, $value) {
	foreach ( $arr as $item ) {
		if ($item === $value) {
			return true;
		}
	}
	return false;
}

/**
 * 判断两个数组的内容是否相同
 *
 * @param unknown $arr1        	
 * @param unknown $arr2        	
 * @return boolean
 */
function array_is_same($arr1, $arr2) {
	if (count ( $arr1 ) !== count ( $arr2 )) {
		return false;
	}
	
	foreach ( $arr1 as $item1 ) {
		if (! array_contain_value ( $arr2, $item1 )) {
			return false;
		}
	}
	
	foreach ( $arr2 as $item2 ) {
		if (! array_contain_value ( $arr1, $item2 )) {
			return false;
		}
	}
	return true;
}

/**
 * 判断数组中是否有相同的元素
 *
 * @param unknown $arr        	
 */
function array_contain_same_value($arr) {
	for($i = 0; $i < count ( $arr ); $i ++) {
		for($j = 0; $j < count ( $arr ); $j ++) {
			if ($i != $j && $arr [$i] === $arr [$j]) {
				return true;
			}
		}
	}
	return false;
}

/**
 * 判断一个字符串为空或null
 *
 * @param unknown $string        	
 * @return boolean
 */
function is_empty_or_null_string($string) {
	return is_null ( $string ) || ! is_string ( $string ) || ($string === '');
}

/**
 * 判断一个变量是否是数字
 *
 * @param unknown $num        	
 * @return boolean
 */
function is_num($num) {
	return ! is_null ( $num ) && (is_float ( $num ) || is_double ( $num ) || is_int ( $num ));
}
/**
 * 判断一个变量是否是数字形式
 *
 * @param unknown $num        	
 * @return boolean
 */
function is_num_or_numstr($num) {
	return is_numeric ( $num ) || is_num ( $num );
}